package com.shine.chemicalWholeProcessSystem.ui.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.shine.chemicalWholeProcessSystem.R;
import com.shine.chemicalWholeProcessSystem.bean.RiskManagementBean;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.baseadapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.baseadapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;

public class HiddenTroubleInvestigationListAdapter extends BGARecyclerViewAdapter<RiskManagementBean> {

    private boolean isChooseMore = false;

    public void setChooseMore(boolean isChooseMore) {
        this.isChooseMore = isChooseMore;
    }

    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();

    public HiddenTroubleInvestigationListAdapter(RecyclerView recyclerView, int defaultItemLayoutId) {
        super(recyclerView, defaultItemLayoutId);
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper, int viewType) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_edit);
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    protected void fillData(BGAViewHolderHelper helper, int position, RiskManagementBean model) {
        BGASwipeItemLayout swipeItemLayout = helper.getView(R.id.sil_item_bgaswipe_root);
        TextView number = helper.getView(R.id.number);
        ImageView chooseImg = helper.getView(R.id.chooseImg);

        if (isChooseMore) {
            number.setVisibility(View.GONE);
            chooseImg.setVisibility(View.VISIBLE);
            swipeItemLayout.setSwipeAble(false);
        } else {
            number.setVisibility(View.VISIBLE);
            chooseImg.setVisibility(View.GONE);
            swipeItemLayout.setSwipeAble(true);
        }
        if (model.isChoose()) {
            chooseImg.setImageResource(R.mipmap.ic_choose_on);
        } else {
            chooseImg.setImageResource(R.mipmap.ic_choose_off);
        }
    }

    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }

}
